#include "Arr_Queue.h"
#include <stdlib.h>

extern Arr_Queue *InitQueue(int size)
{
    if(size <= 0)
    {
        return NULL;
    }
    Arr_Queue *newQueue = (Arr_Queue *)malloc(sizeof(Arr_Queue));
    newQueue->tailIndex = 0;
    newQueue->headIndex = -1;
    newQueue->curLength = 0;
    newQueue->size = size;
    newQueue->basicArr = (ArrQueueNode *)malloc(sizeof(ArrQueueNode) * size);
    return newQueue;
}

extern void Enqueue(Arr_Queue *queue, ElementType element)
{
    if(queue == NULL || queue->curLength == queue->size)
    {
        return;
    }
    ArrQueueNode newNode;
    newNode.element = element;
    if(queue->headIndex < 0)
    {
        queue->headIndex = 0;
    }
    queue->basicArr[queue->tailIndex] = newNode;
    ++queue->curLength;
    if(++queue->tailIndex >= queue->size)
    {
        queue->tailIndex = 0;
    }
}

extern ElementType Dequeue(Arr_Queue *queue)
{
    if(queue == NULL ||  queue->curLength <= 0)
    {
        return NULL;
    }
    ElementType peekElem = queue->basicArr[queue->headIndex].element;
    if(++queue->headIndex >= queue->size)
    {
        queue->headIndex = 0;
    }
    --queue->curLength;
    return peekElem;
}

extern ElementType Peek(Arr_Queue *queue)
{
    if(queue == NULL || queue->headIndex < 0)
    {
        return NULL;
    }
    return queue->basicArr[queue->headIndex].element;
}

extern void ShowQueue(Arr_Queue *queue)
{
    for (int j = queue->headIndex; j != queue->tailIndex;)
    {
        printf("%d", queue->basicArr[j].element);
        if(++j >= queue->size)
        {
            j = 0;
        }
    }
}

extern void Clear(Arr_Queue *queue)
{
    queue->headIndex = -1;
    queue->tailIndex = -1;
}